package com.whstudio.article;

import java.sql.SQLException;

import com.whstudio.util.mvc.action.ActionUtil;
import com.whstudio.util.mvc.bo.AbstractBO;
import com.whstudio.util.xml.IDocument;

public class ArticleBO extends AbstractBO {
    /**
     * 保存信息
     * 
     * @param document
     * @return
     */
    public IDocument saveArticle(IDocument document) {
        UserVO user = new UserVO();
//        document.element2Map();
        user.setUsername(document.getFirstElementByTagName("user").getElementsByTagName("userName").item(0).getTextContent());
        user.setDepartment(document.getFirstElementByTagName("user").getElementsByTagName("department").item(0).getTextContent());
        user.setAddress(document.getFirstElementByTagName("user").getElementsByTagName("address").item(0).getTextContent());
        user.setEmail(document.getFirstElementByTagName("user").getElementsByTagName("email").item(0).getTextContent());
        
        ArticleVO article = new ArticleVO();
        article.setUserName(user.getUsername());
        article.setArticleSubject(document.getFirstElementByTagName("article").getElementsByTagName("articleSubject").item(0).getTextContent());
        article.setArticleContent(document.getFirstElementByTagName("article").getElementsByTagName("articleContent").item(0).getTextContent());
        
        ArticleDAO articleDAO = new ArticleDAO();
        try {
            articleDAO.registerUser(user);
        } catch (SQLException e) {
            return ActionUtil.getErrorDoc("articleDAO.registerUser:" + e.getMessage());
        }
        try {
            articleDAO.doSaveArticle(article);
        } catch (SQLException e) {
            return ActionUtil.getErrorDoc("articleDAO.doSaveArticle:" + e.getMessage());
        }
        return ActionUtil.getPromptDoc("1", "成功");
    }
    
    /**
     * 查询所有的求助帖
     * @param document
     * @return
     */
    public IDocument doQueryArticles(IDocument document){
        ArticleDAO articleDAO = new ArticleDAO();
        try {
            return articleDAO.doQueryArticles();
        } catch (SQLException e) {
            return ActionUtil.getErrorDoc(e.getMessage());
        }
    }
}
